package com.yuantong.xyerp.core.provider.dao;

import com.yuantong.xyerp.core.api.dto.ContractDto;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 合同基本信息/采购单/销售单，合同审批按前端销售/采购
发票作为合同附件 Mapper 接口
 * </p>
 *
 * @author JianguoYin
 * @since 2021-07-21
 */
public interface ContractMapper{

    // getContract(); // <script></script>


    //关联 merchant_id, warehouse_id, 销售人员signBy, 创建人name 业务类型：B-采购；S-销售  用来复用 采购或销售 导出

    /**
     *
     * @param businessType 业务类型
     * @param stateType 审核状态
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @param fuzzy 模糊查询参数
     * @param lowerAmount
     * @param higherAmount

     * @return
     */
    @Select({"<script> " +
            "SELECT c.*, " +
            "m.name AS merchant_name, o.name AS warehouse_name, u.real_name AS sign_name, u2.real_name AS creator_name, " +
            "cat.name AS category_name, " +
            "(c.total_payment - c.actual_payment) AS actual_receivable, " +
            "wr.total_weight as actual_weight " +
            "FROM t_contract c " +
            "LEFT JOIN t_merchant m ON (m.id = c.merchant_id) " +
            "LEFT JOIN t_organization o ON (o.id = c.warehouse_id) " +
            "LEFT JOIN t_user u ON (u.id = c.sign_by) " +
            "LEFT JOIN t_user u2 ON (u2.id = c.creator_id) " +
            "LEFT JOIN t_category cat ON (cat.code = c.category_code) " +
            "LEFT JOIN t_warehouse_receipt wr ON (wr.business_id = c.id) " +
            "WHERE 1=1 AND c.delete_status = 1 " +
            "<if test=\"businessType != null and businessType != ''\"> " +
            "    AND c.business_type = #{businessType} " +
            "</if>" +
            "<if test=\"stateType != null and stateType != ''\"> " +
            "    AND c.next_process_state = #{stateType} " +
            "</if>" +
            "<if test=\"startTime != null\"> " +
            "  AND c.create_time &gt;= #{startTime} " +
            "</if>" +
            "<if test=\"endTime != null\"> " +
            "  AND c.create_time &lt;= #{endTime} " +
            "</if> " +
            "<if test=\"fuzzy != null and fuzzy != ''\"> " +
            "<bind name=\"bindKey\" value=\"'%'+fuzzy+'%'\"/> " +
            "AND (c.contract_no LIKE #{bindKey} OR c.contacts_name LIKE #{bindKey} OR sign_name LIKE #{bindKey}) " +  // sign_name
            "</if>" +
            "<if test=\"lowerAmount != null\"> " +
            "  AND c.total_payment &gt;= #{lowerAmount} " +
            "</if>" +
            "<if test=\"higherAmount != null\"> " +
            "  AND c.total_payment &lt;= #{higherAmount} " +
            "</if> " +
            "<if test=\"causeId != null and causeId != ''\"> " +   // 个人数据
            "    AND c.sign_by = #{causeId} " +
            "</if>" +
            "<if test=\"causeId != null and causeId != ''\"> " + // 部门数据
            "    AND c.warehouse_id = #{causeId} " +
            "</if>" +
            "<if test=\"ids != null and ids.size() > 0\"> " +
            "AND c.id IN " +
            "<foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\"> " +
            "#{id} " +
            "</foreach> " +
            "</if> " +
            "ORDER BY c.create_time DESC " +
            "</script>"})
    @ResultMap("ContractDtoResult")
    List<ContractDto> findAllByFuzzy(
            String businessType,
            String stateType, Date startTime, Date endTime, String fuzzy,
            BigDecimal lowerAmount, BigDecimal higherAmount, List<String> ids, String causeId);


}
